package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportCompany;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportCompanyDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-05-18 20:14:03
 */
public class TransportCompanyConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportCompanyConverts.class);

    public static List<TransportCompanyDTO> convert(List<TransportCompany> transportCompanyList) {
        if (CollectionUtils.isEmpty(transportCompanyList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportCompanyDTO> transportCompanyListResult = Lists.newArrayList();
        for (TransportCompany transportCompany : transportCompanyList) {
            transportCompanyListResult.add(convert(transportCompany));
        }
        return transportCompanyListResult;
    }

    public static TransportCompanyDTO convert(TransportCompany transportCompany) {
        try {
            return BeanUtils.copy(transportCompany, TransportCompanyDTO.class);
        } catch (Exception e) {
            LOG.error("TransportCompany到TransportCompanyDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportCompany到TransportCompanyDTO转换失败", e);
        }
    }

    public static TransportCompany convert(TransportCompanyDTO transportCompanyDTO) {
        try {
            return BeanUtils.copy(transportCompanyDTO, TransportCompany.class);
        } catch (Exception e) {
            LOG.error("TransportCompanyDTO到TransportCompany转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportCompanyDTO到TransportCompany转换失败", e);
        }
    }
}